const loadGruntTasks = require('load-grunt-tasks')
const sass = require('sass')
module.exports = grunt => {
    grunt.initConfig({
        sass: {
            options: {
                implementation: sass
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: ["assets/styles/*.scss"],
                    dest: "dist/",
                    ext: ".css"
                }]
            }
        },
        babel: {
            options: {
                presets: ['@babel/preset-env']
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: ["assets/scripts/*.js"],
                    dest: "dist/",
                }]
            }
        },
        web_swig: {
            options: {
              swigOptions:{
                cache: false
              },
              getData: function(tpl){
                  return {title: 'lagou'}; // 没有data数据，随便写了一个
              }
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: 'src',
                    src: ["*.html"],
                    dest: "dist/",
                }]
            },
        },
        // 拷贝public下的文件
        copy_public: { 
            dist: {
                expand: true,
                src: 'public/**',
                dest: 'dist/'
            }
        },
        // 监听源文件并热编译
        watch: {
            css: {
                files: ['src/assets/styles/*.scss'],
                tasks: ['sass'],
            },
            scripts: {
                files: ['src/assets/scripts/*.js'],
                tasks: ['babel'],
            },
            html: {
                files: ['src/**/*.html'],
                tasks: ['web_swig'],
            }
        },
        imagemin: {
            dist_image: {
                options: {
                    optimizationLevel: 3
                },
                files: [{
                    expand: true,
                    cwd: 'src/',
                    src: ['assets/images/**'],
                    dest: 'dist/' 
                }]
            },
            dist_font: {
                options: {
                    optimizationLevel: 3
                },
                files: [{
                    expand: true,
                    cwd: 'src/',
                    src: ['assets/fonts/**'],
                    dest: 'dist/' 
                }]
            }
        },
        htmlmin: {
            options: {
                removeComments: true, //移除注释
                removeCommentsFromCDATA: true,//移除来自字符数据的注释
                collapseWhitespace: true,//无用空格
                collapseBooleanAttributes: true,//失败的布尔属性
                removeAttributeQuotes: true,//移除属性引号      有些属性不可移走引号
                removeRedundantAttributes: true,//移除多余的属性
                useShortDoctype: true,//使用短的跟元素
                removeEmptyAttributes: true,//移除空的属性
                removeOptionalTags: true//移除可选附加标签
            },
            dist: {
                files: [{
                    expand: true,
                    cwd: 'dist/',
                    src: ['*.html'],
                    dest: 'dist/' 
                }]
            }
        },
        useref: {
            html: 'dist/*.html',
            temp: 'dist'
        },
        // 监听编译后的文件是否改变，并热更新浏览器
        browserSync: {
            dev: {
                bsFiles: {
                    src : [
                        'dist/assets/styles/*.css',
                        'dist/assets/scripts/*.js',
                        'dist/*.html'
                    ]
                },
                options: {
                    watchTask: true,
                    server: {
                        baseDir: ['dist', 'src', 'public'],
                        routes: {
                            '/node_modules': 'node_modules'
                        }
                    }
                }
            }
        }
    })
    loadGruntTasks(grunt)
    grunt.registerTask('compile', ['sass', 'babel', 'web_swig'])
    grunt.registerTask('dev', ['compile', 'browserSync', 'watch'])
    grunt.registerTask('build', ['compile', 'useref', 'concat', 'uglify', 'cssmin'])
}